package android;


import hibernate.Driverstab;
import hibernate.DriverstabDAO;

import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONObject;





import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;



@ServerEndpoint(value = "/android/WebsocketAction")
public class webSocketEndPoint extends Endpoint{
	private  static HashMap<Integer, RemoteEndpoint.Basic> userMap=new HashMap<Integer, RemoteEndpoint.Basic>();

	//将此session从map中删除
	@Override
	public void onClose(Session session, CloseReason closeReason) {
		Set<Integer> keySet=userMap.keySet();
		RemoteEndpoint.Basic remote=session.getBasicRemote();
		for (Integer key : keySet) {
			if(userMap.get(key).equals(remote))
			{
				userMap.remove(key);
				break;
			}
		}


		System.out.println("onClose");
	}

	@Override
	public void onError(Session session, java.lang.Throwable throwable) {
		System.out.println("onError");
	}

	@Override
	public void onOpen(Session session, EndpointConfig config) {

		RemoteEndpoint.Basic remote =  session.getBasicRemote();
		System.out.println("pathParams:"+session.getPathParameters());
		System.out.println("requestParams"+session.getRequestParameterMap());
		session.addMessageHandler(new MyMessageHandle(remote));

	}
 //将id和message放入map
	private void doLogin (JSONObject jsonObj,RemoteEndpoint.Basic remote)throws Exception
	{
		String account=jsonObj.getString("account");

		List<Driverstab> driversList=null;
		DriverstabDAO driverstabDAO=new DriverstabDAO();
		driverstabDAO.getSession().beginTransaction();
		try
		{

			String hql="from Driverstab as d where d.account like :account";
			Query result=driverstabDAO.getSession().createQuery(hql);

			result.setString("account",account);
			driversList=result.list();
		}
		catch(HibernateException e)
		{
			System.out.println("hibernate error in websocket login");
			e.printStackTrace();
		}
		finally
		{
			driverstabDAO.getSession().getTransaction().commit();
			driverstabDAO.getSession().close();

		}

		userMap.put(driversList.get(0).getUuid(),remote);


		JSONObject resultJson=new JSONObject();
		resultJson.put("op","login");
		resultJson.put("result","succeed");


		remote.sendText(resultJson.toString());

	}



	public static  void onOrderAccept (int userid,int parkid,int orderid)
	{
		try
		{
			RemoteEndpoint.Basic remote=userMap.get(userid);
			JSONObject resultJson=new JSONObject();
			resultJson.put("op","orderAccept");
			remote.sendText(resultJson.toString());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}


//客户端发送的消息中如果含有login则执行dologin操作
	private class MyMessageHandle implements MessageHandler.Whole<String> {
		RemoteEndpoint.Basic remote =  null;
		public MyMessageHandle(RemoteEndpoint.Basic remote){
			this.remote = remote;
		}

		@Override
		public void onMessage(String s) {
			System.out.println(s);
			try {

				JSONObject jsonObj=new JSONObject(s);
				String op=jsonObj.getString("op");

				if("login".equals(op))
				{
					doLogin(jsonObj,remote);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
